#!usr/bin/env python  
# -*- coding:utf-8 _*-
""" 
@author:robot
@file: api04_task.py 
@version:
@time: 2022/03/10 
"""
from TapClientCore.T02_tm_api.v2_api.v2_api_base import *


class Api04TaskUrl(V2ApiUrlBase):
    url_task = api_joint(V2ApiUrlBase().server, Task)
    url_find_task_detail_by_id = api_joint(url_task, FindTaskDetailById)
    url_task_confirm = api_joint(url_task, TaskConfirm)
    url_task_batch_start = api_joint(url_task, TaskBatchStart)
    url_task_batch_stop = api_joint(url_task, TaskBatchStop)
    url_task_batch_delete = api_joint(url_task, TaskBatchDelete)
    url_task_tran_model_version_control = api_joint(url_task, TasktranModelVersionControl)
    url_task_check_name = api_joint(url_task, TaskCheckName)
    url_task_metadata = api_joint(url_task, TaskMETA)
    url_task_renew = api_joint(url_task, 'batchRenew')


class Api04Task(V2ApiBase):

    def get_task_by_id(self, task_id):
        response = self.tapclient(
            self.get,
            api_joint(Api04TaskUrl.url_task, task_id) + self.access_token
        )
        return response

    def get_task_by_filter(self, filter):
        response = self.tapclient(
            self.get,
            Api04TaskUrl.url_task + self.access_token + self.filter(filter)
        )
        return response

    def get_task_find_task_detail_by_id(self, task_id):
        response = self.tapclient(
            self.get,
            api_joint(
                Api04TaskUrl.url_find_task_detail_by_id,
                task_id
            )
            + self.access_token
        )
        return response

    def post_task_check_name(self, task_name):
        check_name_str = f'&name={task_name}'
        response = self.tapclient(
            self.post,
            Api04TaskUrl.url_task_check_name + self.access_token + check_name_str
        )
        return response

    def post_task_tran_model_version_control(self, payload):
        """
        判断源和目标是否支持字段映射
        :param payload:
        :return:
        """
        response = self.tapclient(
            self.post,
            Api04TaskUrl.url_task_tran_model_version_control,
            json=payload
        )
        return response

    def patch_task_confirm(self, payload, task_id=None):
        """
        创建任务或修改任务
        :param task_id:
        :param payload:
        :return:
        """
        url = Api04TaskUrl.url_task_confirm
        if task_id:
            url = api_joint(url, task_id)
        response = self.tapclient(
            self.patch,
            url + self.access_token,
            json=payload
        )
        return response

    def put_task_start_by_id(self, task_id):
        response = self.tapclient(
            self.put,
            Api04TaskUrl.url_task_batch_start
            + f'?{TaskIds}={task_id}'
            + self.access_token2
        )
        return response

    def put_task_stop_by_id(self, task_id):
        response = self.tapclient(
            self.put,
            Api04TaskUrl.url_task_batch_stop
            + f'?{TaskIds}={task_id}'
            + self.access_token2
        )
        return response

    def delete_task_by_id(self, task_id):
        response = self.tapclient(
            self.delete,
            Api04TaskUrl.url_task_batch_delete
            + f'?{TaskIds}={task_id}'
            + self.access_token2
        )
        return response

    def post_task_metadata(self, payload):
        response = self.tapclient(
            self.post,
            Api04TaskUrl.url_task_metadata + self.access_token,
            json=payload
        )
        return response

    # 重置任务
    def patch_task_renew(self, task_id):
        response = self.tapclient(
            self.patch,
            Api04TaskUrl.url_task_renew
            + f'?taskIds={task_id}'
            + self.access_token2
        )
        return response


if __name__ == '__main__':
    test = Api04Task()
    res = test.patch_task_renew('62b0178b9d814e28f75829a3')
    print(res)
